﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ProdeManagmente.DAO;
using ProdeManagmente.Model;
using DgvFilterPopup;
using ProdeManagmente.Libs;

namespace ProdeManagmente.Forms.ABMS
{
    public partial class FrmListMatch : Form
    {
        private static FrmListMatch instance;
        private MatchService matchService;
        private ResultService resultService;

        private FrmListMatch()
        {
            InitializeComponent();
            matchService = MatchService.getMatchServiceInstance();
            resultService = ResultService.getResultServiceInstance();
            LoadForm();
        }

        public static FrmListMatch getFrmListMatchInstance()
        {
            if (instance == null)
            {
                instance = new FrmListMatch();
            }
            instance.Select();
            return instance;
        }

        private void LoadForm()
        {
            List<Match> matchList = new List<Match>();
            matchService = MatchService.getMatchServiceInstance();
            matchList = matchService.getMatchListFilter("");


            DataTable dataTable = new DataTable();
            dataTable.Columns.AddRange(new DataColumn[7] {
                                new DataColumn("ID",typeof(int)),
                                new DataColumn("TORNEO",typeof(string)),
                                new DataColumn("EQUIPO 1",typeof(string)),
                                new DataColumn("EQUIPO 2",typeof(string)),
                                new DataColumn("FECHA",typeof(DateTime)),
                                new DataColumn("HORA",typeof(String)),
                                new DataColumn("RESULTADO",typeof(bool))});

            foreach (Match match in matchList)
            {
                
                dataTable.Rows.Add(match.getId(), match.getTournament().getName(), 
                                   match.getTeam1().getName(), match.getTeam2().getName(),
                                   match.getDate(), match.getTime().ToString("t"),
                                   match.hasResult);
            }

            dataGridView1.DataSource = dataTable;
            DgvFilterManager filterManager = new DgvFilterManager(dataGridView1);
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmMatch frmMatch = new FrmMatch(null);
            frmMatch.ShowDialog();
            LoadForm();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            long idMatch = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());

            Match match = matchService.getMatchById(idMatch);
            FrmMatch frmMatch = new FrmMatch(match);
            frmMatch.ShowDialog();
            LoadForm();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Desea borrar el elemento?", "Salir", MessageBoxButtons.YesNo);

            if (result == DialogResult.Yes)
            {
                try
                {
                    long idTeam = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    matchService.delete(idTeam);
                }
                catch (BusinessException error)
                {
                    new MensajeSalida(error).showMensaje();
                }

                LoadForm();
            }
        }

        private void FrmListMatch_FormClosed(object sender, FormClosedEventArgs e)
        {
            instance = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            resultService = ResultService.getResultServiceInstance();

            long idMatch = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            Result result = resultService.getResultByIdMatch(idMatch);
            if (result == null)
            {
                Match match = matchService.getMatchById(idMatch);
                result = new Result(match, -1, -1, -1, -1, false);
            }
            FrmResult frmResult = new FrmResult(result);
            frmResult.ShowDialog();
        }
    }
}
